<?php

class Group extends Auth_Controller{
	function __construct() {
		parent::__construct();
//		$this->myauth->execute_auth ();
//		$this->load->model ( 'acl/GroupModel', '', true );
//		$this->load->model ( 'acl/UserGroupModel', '', true );
//		$this->load->model ( 'acl/GroupPermissionModel', '', true );
	}

	/**
	 * 权限分组页面
	 */
	function index(){
		$this->load->view('manager/acl/group');
	}
	/**
	 * 权限列表
	 */
	function indexlist(){
		$data['list'] = $this->GroupModel->get_groups_alls();
		$this->load->view('acl/groups_inc',$data);
	}
	/**
	 * 权限分组添加页面
	 */
	function add(){
		$this->load->view('acl/groups_add');
	}
	/**
	 * 保存
	 */
	function doadd(){
		$group['name'] = $this->input->post ( "names", true );
		$group['sortid'] = $this->input->post ( "sortid", true );
		$group['utype'] = 2;
		$this->form_validation->set_rules($this->setUserRules());
		if ($this->form_validation->run () == FALSE) {
			echo 'falid';
		} else {
			if($this->GroupsModel->exist_groups($group['name'],null)>0){
				echo 'falid';
			}else{
				$this->GroupsModel->add_groups($group);
				redirect ( 'acl/groups/indexlist' );
			}

		}
	}
	/**
	 * 验证规则
	 */
	function setUserRules() {
		return array (array ("label" => "排序",
                             "field" => "sortid",
                             "rules" => "required|integer" ),
		array ("label" => "组名",
                             "field" => "names",
                             "rules" => "required" )
		);
	}

	/**
	 * 删除权限
	 */
	function delete(){
		$group['groupid'] = $this->input->post('groupid',true);
		$ug = $this->UserGroupModel->get_userGroup_byGroupids($group['groupid']);
		$gp = $this->GroupPermissionModel->get_gourppermission_id($group['groupid']);

		if($ug>0||$gp>0){
			echo 'falid';
		}else{
			$result = $this->GroupsModel->del_groups($group['groupid']);
			$data['result']='';
			$data['message']='';
			if(!$result){
				$data['result']='falid';
				$data['message'] = '删除失败！';
			}
			redirect ( 'acl/groups/indexlist',$data );
		}

	}

	/**
	 * 修改页面
	 */
	function edit(){
		$group['groupid'] = $this->input->post('groupid',true);
		$temp= $this->GroupsModel->get_groups_id($group['groupid']);
		$data['group'] = array('groupid'=>$temp->groupid,'name'=>$temp->name,'sortid'=>$temp->sortid);
		$this->load->view('acl/groups_edit',$data);
	}

	/**
	 * 修改
	 */
	function doedit(){
		$group['groupid'] = $this->input->post('groupid',true);
		$group['name'] = $this->input->post('names',true);
		$group['sortid'] = $this->input->post('sortid',true);
		$this->form_validation->set_rules($this->setUserRules());
		if ($this->form_validation->run () == FALSE) {
			//echo validation_errors();
			echo 'falid';
		} else {
			if($this->GroupsModel->exist_groups($group['name'],$group['groupid'])>0){
				echo 'falid';
			}else{
				$this->GroupsModel->update_groups($group);
				redirect ( 'acl/groups/indexlist' );
			}

		}
	}

}
?>
